<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${packageName}.dao.${className}Dao" > 
<!-- Result Map-->
<resultMap id="${className}ResultMap" type="${packageName}.vo.${className}" >
<#list attrs as a>
	<result column="${a.columnName}" property="${a.columnName}"/>
</#list>
<#if childInfos??>
<#list childInfos?keys as childClassName>
<#assign child = childInfos[childClassName] />
	<collection column="{${child.refcolumnName}}" select="${packageName}.dao.${className}Dao.find${childClassName}sByParentId" property="${childClassName?uncap_first}s" fetchType="lazy"/>
</#list>
</#if>
</resultMap>

<#if tableInfos??>
<#list tableInfos?keys as childClassName>
<#assign item = tableInfos[childClassName]>
<resultMap id="${childClassName}ResultMap" type="${packageName}.vo.${childClassName}" >
<#list item as child>
<result column="${child.columnName}" property="${child.columnName}"/>
</#list>
</#list>
</resultMap>

<#list tableInfos?keys as childClassName>
<#assign item = tableInfos[childClassName]>
<select id="find${childClassName}sByParentId"  resultMap="${childClassName}ResultMap" parameterType="Object">
   select
<#list item as child>  
	${child.columnName}<#if child_has_next>,</#if >
</#list> 
<#assign childinfo = childInfos[childClassName] />
from ${childinfo.tableName} where ${childinfo.columnName} = ${r'#{'}${childinfo.refcolumnName}${r'}'}
</select>
</#list>
</#if>

<sql id="base_sql">
<#list attrs as a> 
	${a.columnName}<#if a_has_next>,</#if >
</#list>
</sql>

<!-- 查询条件 -->
<sql id="Base_Where_Sql">
	<where>
		<#list attrs as a>
			<if test="${a.columnName} != null and ${a.columnName} != ''">
				AND ${a.columnName} = ${r'#{'}${a.columnName}${r'}'}
			</if>
		</#list>
	</where>
</sql>

<select id="query"  resultMap="${className}ResultMap" parameterType="Object">
		select 
		<include refid="base_sql"></include> 
		from ${tableName}
		<include refid="Base_Where_Sql"></include>
		limit ${r'${page-1}'},${r'${limit}'}
</select>

<select id="query${className}Count" parameterType="Object" resultType="int">
	select count(1) from ${tableName}
	<include refid="Base_Where_Sql"></include>
</select>

<select id="queryById" resultMap="${className}ResultMap" parameterType="Object">
	select
	<include refid="base_sql"></include> 
		from ${tableName}
		where 
		<#list attrs as a>
			<#if a.primary?contains("TRUE")>
				<if test="${a.columnName} != null and ${a.columnName} != ''">
					${a.columnName} = ${r'#{'}${a.columnName}${r'}'}
				</if>
			</#if> 
		</#list>
</select>

<delete id="delete" parameterType="Object">
	delete ${tableName} 
	<include refid="Base_Where_Sql"></include>
</delete>

<insert id="insert" parameterType="Object">
	insert into ${tableName}(
		<#list attrs as a>
			<#if a.primary?contains("FALSE")>
			<if test="${a.columnName} != null">
				${a.columnName}<#if a_has_next>,</#if >
			</if>
			</#if>
		</#list>	
	 ) 
	values (
		<#list attrs as a>
			<#if a.primary?contains("FALSE")>
			<if test="${a.columnName} != null">
				${r'#{'}${a.columnName},jdbcType=${a.columnType}${r'}'}<#if a_has_next>,</#if>
			</if>
			</#if>
		</#list>
	)
</insert>

<update id="update" parameterType="Object">
	update ${tableName}
	<set>
		<#list attrs as a>
			<#if a.primary?contains("FALSE")>
				<if test="${a.columnName} != null">
					${a.columnName} = ${r'#{'}${a.columnName},jdbcType=${a.columnType}${r'},'}
				</if>
			</#if> 
		</#list>
	</set>
	<where>
		<#list attrs as a>
			<#if a.primary?contains("TRUE")>
				${a.columnName}=${r'#{'}${a.columnName}${r'}'}
			</#if> 
		</#list>
	</where>
</update>
</mapper>   
